<template>
    <div class="import-content">
        <div>
            <div class="import-btn">
                <el-button v-if="fileImpParam.fileimportid" @click="changeModel">修改模板</el-button>
                <el-button v-if="!fileImpParam.fileimportid" @click="addModel">新增模板</el-button>
                <el-button @click="importReqSample">导入</el-button>
                <el-button @click="closeReqImport">取消</el-button>
            </div>
            <div class="import-info">
                <div class="import-param">
                    <div class="search-title" style="width: 48px;">导入模板</div>
                    <div>
                        <el-select v-model="fileImpParam.fileimportid" placeholder="请选择模板" clearable @change="changeFileimport" style="width:130px">
                            <el-option v-for="(item,index) in fileImportList" :key="index" :value="item.fileimportid" :label="item.fileimportname"></el-option>
                        </el-select>
                    </div>
                    <!-- <div style="width: 210px;line-height: 26px;color: #1476a5;font-size: 13px;font-weght: bolder;">(清除模板后点击修改即可新增模板)</div> -->
                    <!-- <div class="search-title">文件类型</div>
                    <div>
                        <el-input size="small" style="width:130px;" v-model="fileImpParam.filetype" disabled></el-input>
                    </div> -->
                    <!-- <div class="search-title">对应方式</div>
                    <div>
                        <el-input size="small" style="width:130px;" v-model="fileImpParam.coltype" disabled></el-input>
                    </div> -->
                    <div class="search-title">数据起始行</div>
                    <div>
                        <el-input size="small" style="width:130px;" v-model="fileImpParam.startrow" disabled></el-input>
                    </div>
                    <div class="search-title" style="width: 62px;">对应方式</div>
                    <div>
                        <!-- <el-input size="small" style="width:130px;" v-model="fileImpParam.coltype" disabled></el-input> -->
                        <el-select v-model="fileImpParam.coltype" disabled style="width:130px;">
                            <el-option label="按顺序对应" value="0"></el-option>
                            <el-option label="按名称对应" value="1"></el-option>
                        </el-select>
                    </div>
                </div>
                <div class="import-param">
                    <div class="search-title" style="width: 48px;">备注</div>
                    <div style="width: calc(100% - 53px);">
                        <el-input size="small" style="width:100%;" v-model="fileImpParam.note" disabled></el-input>
                    </div>
                </div>
            </div>
            <div class="import-info">
                <span class="model-text" style="width: 48px;">文件</span>
                <el-input class="input-upload" size="small" v-model="filename" disabled></el-input>
                <el-button icon="el-icon-files" @click="importExcel" ref="importFile">选择文件</el-button>
                <div class="upload-button">
                    <el-upload ref="upload" :action="url" accept=".xls,.xlsx" :before-upload="beforeUploadFile" :on-change="fileChange" :show-file-list="showFile" :auto-upload="false">
                        <el-button size="small" plain>选择文件</el-button>
                        <!-- <div slot="tip" class="el-upload__tip">只能上传DAT(*dat)文件，且不超过10M</div> -->
                    </el-upload>
                </div>
            </div>
            <div v-if="allowaddreqitemflag == '1'" class="import-info">
                <!-- <div class="import-info"> -->
                <div class="reqitem-content">
                    <div style="width: 490px;">
                        <div style="height: 30px;line-height: 30px;">附加申请项目：</div>
                        <div class="reqitem-table">
                            <vxe-table show-overflow ref="reqitemTable" :data.sync="reqitemsList" height="auto" width="100%" size="mini" border show-header-overflow header-align="center" highlight-current-row resizable stripe :sort-config="{trigger: 'cell'}" :edit-config="{ trigger: 'click', mode: 'cell', showStatus: true, showIcon: true, activeMethod: activeMethod }">
                                <vxe-table-column field="req_itemcode" title="申请项目代码" width="100px" :edit-render="{name:'input', autoselect: true}">
                                    <!-- <template v-slot:edit="{row}">
                                        <rm-selecttable ref="tablereqselect" :currentLable="row.req_itemname" :showName="showName" :tableColumnData="allReqsheetTable" :ftableDataList="fallReqsheetList" :filterColumn="filterColumn" @change="changeReqitem($event, row)"></rm-selecttable>
                                    </template>
                                    <template v-slot="{row}">
                                        <span>{{getShowName(row.req_itemcode, "allReqsheetList")}}</span>
                                    </template> -->
                                    <template v-slot:edit="{ row }">
                                        <el-input v-model="row.req_itemcode" @keyup.native="reqitemFocus($event, row)" @focus="reqitemFocus($event, row)" @blur="reqitemBlur($event, row)"></el-input>
                                    </template>
                                </vxe-table-column>
                                <vxe-table-column field="req_itemname" title="申请项目名称" width="200px"></vxe-table-column>
                                <vxe-table-column field="specimen_name" title="标本类型" width="100px"></vxe-table-column>
                            </vxe-table>
                        </div>
                    </div>
                    <div class="reqitem-btn">
                        <div>
                            <el-button @click="addReqitem">增加</el-button>
                        </div>
                        <div>
                            <el-button @click="deleteReqitem">删除</el-button>
                        </div>
                        <div>
                            <el-button @click="addReqpackage">选择套餐</el-button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <el-dialog width="auto" v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="文件导入模板" v-if="modelSetDialog" :visible.sync="modelSetDialog" @closed="closeModelSet">
            <div class="modelset-content">
                <div class="modelset-left">
                    <div class="model-param">
                        <span class="model-text">导入模板ID</span>
                        <el-input size="small" style="width:130px;" v-model="fileImpParam.fileimportid" disabled></el-input>
                    </div>
                    <div class="model-param">
                        <span class="model-text">导入目标</span>
                        <el-input size="small" style="width:130px;" v-model="fileImpParam.importdest" disabled></el-input>
                    </div>
                    <div class="model-param">
                        <span class="model-text">模板名称</span>
                        <el-input size="small" style="width:130px;" v-model="fileImpParam.fileimportname"></el-input>
                    </div>
                    <div class="model-param">
                        <span class="model-text">文件类型</span>
                        <el-input size="small" style="width:130px;" v-model="fileImpParam.filetype" disabled></el-input>
                    </div>
                    <div class="model-param">
                        <span class="model-text">数据起始行</span>
                        <el-input type="number" min="0" size="small" style="width:130px;" v-model="fileImpParam.startrow"></el-input>
                    </div>
                    <div class="model-param">
                        <span class="model-text">对应方式</span>
                        <el-select v-model="fileImpParam.coltype" placeholder="请选择对应方式" style="width:130px;">
                            <el-option label="按顺序对应" value="0"></el-option>
                            <el-option label="按名称对应" value="1"></el-option>
                        </el-select>
                    </div>
                    <div class="model-param">
                        <span class="model-text">创建人</span>
                        <rm-ddl-common v-model="fileImpParam.create_user" datatype="doctor" style="display: inline-block;" width="130px" disabled></rm-ddl-common>
                    </div>
                    <div class="model-param">
                        <span class="model-text">创建时间</span>
                        <el-input size="small" style="width:130px;" v-model="fileImpParam.create_dt" disabled></el-input>
                    </div>
                    <div class="model-param">
                        <span class="model-text">备注</span>
                        <el-input size="small" style="width:130px;" v-model="fileImpParam.note"></el-input>
                    </div>
                    <div class="model-param" style="color: #1476a5;">
                        <div>1、选择导入文件，再填写模板名称、起始行、对应方式。</div>
                        <div>2、在右侧进行列对应，如果已经选择过文件，则可以直接下拉文件列名称进行对照</div>
                        <div>3、所有字段都应是代码，程序不会根据名称转换代码</div>
                        <div>4、文件中不存在的列可以填写默认值</div>
                        <div>5、主表唯一标识一定要选，也就是勾选主表的一个列相同就认定为一个申请单。比如勾选条码号。报告导入不需要</div>
                        <div>6、还有其它选项在“选项”页面设置，可以设詈生成新条码号，重新分单，手工追加申请项目等等</div>
                    </div>
                </div>
                <div class="modelset-right">
                    <el-tabs v-model="setFileName" @tab-click="handleTabClick" type="card">
                        <el-tab-pane label="对应列" name="importDet">
                            <div class="eltab-content">
                                <vxe-table ref="detTable" :data.sync="detList" class="mytable-style" height="auto" border size="mini" resizable stripe :sort-config="{trigger: 'cell'}" show-overflow highlight-current-row header-align="center" :edit-config="{ trigger: 'click', mode: 'cell', showStatus: true, showIcon: false}">
                                    <vxe-table-column field="datacol" sortable title="对应数据列" width="170">
                                        <template v-slot="{ row }">
                                            <span>{{row.datacolcn}}</span>
                                            <span>[{{row.datacol}}]</span>
                                        </template>
                                    </vxe-table-column>
                                    <vxe-table-column field="filecolseq" sortable title="文件列序号" width="70" :edit-render="{name: 'input', autoselect: true}">
                                        <template v-slot:edit="{ row }">
                                            <el-select v-model="row.filecolseq" placeholder="" filterable remote :remote-method="fillterDetTableseq" @focus="clearFilter" @change="filecolseqChange($event,row)" allow-create default-first-option clearable :popper-append-to-body="false" popper-class="mini-select" style="width: 100%;">
                                                <el-option v-for="(item,index) in detTableseq" :key="index" :label="item" :value="item"></el-option>
                                            </el-select>
                                        </template>
                                    </vxe-table-column>
                                    <vxe-table-column field="filecolname" sortable title="文件列名称" width="110" :edit-render="{name: 'input', autoselect: true}">
                                        <template v-slot:edit="{ row }">
                                            <!-- <el-input v-model="row.filecolname"></el-input> -->
                                            <el-select v-model="row.filecolname" placeholder="" filterable remote :remote-method="fillterDetTableTitle" @focus="clearFilter" @change="filecolnameChange($event,row)" allow-create default-first-option clearable :popper-append-to-body="false" popper-class="mini-select" style="width: 100%;">
                                                <el-option v-for="(item,index) in detTableTitle" :key="index" :label="item" :value="item"></el-option>
                                            </el-select>
                                        </template>
                                    </vxe-table-column>
                                    <vxe-table-column field="defaultval" sortable title="默认值" width="115" :edit-render="{name: 'input', autoselect: true}">
                                        <template v-slot:edit="{ row }">
                                            <el-input v-model="row.defaultval" @change="updateFdetList($event, row, 'defaultval')"></el-input>
                                        </template>
                                    </vxe-table-column>
                                    <vxe-table-column field="masterkey" sortable title="主表唯一标识" width="55" align="center">
                                        <template v-slot="{ row }">
                                            <el-checkbox v-model="row.masterkey" true-label="1" false-label="0" @change="updateFdetList($event, row, 'masterkey')"></el-checkbox>
                                        </template>
                                    </vxe-table-column>
                                    <vxe-table-column field="fileimportid" sortable title="文件导入模板ID" width="55" align="center"></vxe-table-column>
                                    <vxe-table-column field="datacoltype" sortable title="数据类型" width="60"></vxe-table-column>
                                    <vxe-table-column field="seq" title="序号" width="40" align="center"></vxe-table-column>
                                </vxe-table>
                            </div>
                        </el-tab-pane>
                        <el-tab-pane label="选项/默认值" name="importOption">
                            <div class="eltab-content">
                                <vxe-table ref="optionTable" :data.sync="optionList" class="mytable-style" height="auto" border size="mini" resizable stripe :sort-config="{trigger: 'cell'}" show-overflow highlight-current-row header-align="center" :edit-config="{ trigger: 'click', mode: 'cell', showStatus: true, showIcon: true}">
                                    <vxe-table-column field="optioncode" sortable title="选项代码" width="110"></vxe-table-column>
                                    <vxe-table-column field="optionname" sortable title="选项名称" width="235"></vxe-table-column>
                                    <vxe-table-column field="optionvalue" sortable title="取值" width="80" :edit-render="{name: 'input', autoselect: true}">
                                        <template v-slot:edit="{ row }">
                                            <el-input type="number" min="0" v-model="row.optionvalue"></el-input>
                                        </template>
                                    </vxe-table-column>
                                    <vxe-table-column field="note" sortable title="说明" width="155"></vxe-table-column>
                                    <vxe-table-column field="optionkind" sortable title="选项类别" width="60" align="center"></vxe-table-column>
                                    <vxe-table-column field="fileimportid" sortable title="文件导入模板ID" width="55" align="center"></vxe-table-column>
                                </vxe-table>
                            </div>
                        </el-tab-pane>
                    </el-tabs>
                    <div class="modelset-btn">
                        <el-button @click="importExcelDet">选择文件</el-button>
                        <div class="upload-button">
                            <el-upload ref="uploadDet" :action="url" accept=".xls,.xlsx" :before-upload="beforeUploadFile" :on-change="fileChangeDet" :show-file-list="showFile" :auto-upload="false">
                                <el-button size="small" plain>选择文件</el-button>
                                <!-- <div slot="tip" class="el-upload__tip">只能上传DAT(*dat)文件，且不超过10M</div> -->
                            </el-upload>
                        </div>
                        <el-button @click="autoCompare" style="margin-left: 10px;">智能对照</el-button>
                        <el-button @click="saveSecFileImport">保存</el-button>
                        <el-button @click="closeModelSet(1)">退出</el-button>
                        <el-button v-if="showCompareflag" @click="showCompareData(1)">隐藏未对照</el-button>
                        <el-button v-else @click="showCompareData(2)">显示未对照</el-button>
                    </div>
                </div>
            </div>
        </el-dialog>
        <el-dialog width="940px" v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="套餐选择" v-if="reqsheetDialog" :visible.sync="reqsheetDialog">
            <rm-reqsheet :prono="prono" @change="chooseReqsheet"></rm-reqsheet>
        </el-dialog>
    </div>
</template>

<script>
import XEUtils from "xe-utils";
import reqitem from "@/components/common/reqitem";
import XLSX from "xlsx";
import RmReqsheet from "@/components/rmcompents/rmreq/rm_reqsheet.vue";
import axios from "axios";
export default {
    name: "rm_reqimport",
    components: { RmReqsheet },
    props: {
        prono: {
            type: String,
            default: "",
            require: true,
        },
        importdest: {
            type: String,
            default: "",
            require: true,
        },
        barcodestyle: {
            type: String,
            default: "",
            require: true,
        },
        cxbarcodeprinter: {
            type: String,
            default: "",
            require: true,
        },
    },
    data() {
        return {
            fileImpParam: {
                fileimportid: "",
            },
            allowaddreqitemflag: "0",
            fileImportList: [],
            showFile: false,
            url: "LIS/LIS2/" + this.prono,
            form: {},
            filename: "",
            reqitemsList: [],
            modelSetDialog: false,
            setFileName: "importDet",
            fdetList: [],
            detList: [],
            optionList: reqitem.importOptionList,
            detTableData: [],
            fdetTableseq: [],
            detTableseq: [],
            fdetTableTitle: [],
            detTableTitle: [],
            showCompareflag: true,
            reqsheetDialog: false,
        };
    },
    mounted() {
        this.querySecfileimport();
    },
    methods: {
        querySecfileimport() {
            this.$tool
                .request({
                    url: "LIS/LIS2/" + this.prono,
                    method: "post",
                    data: {
                        action: "querysecfileimport",
                        // prono: "LIS251",
                    },
                })
                .then((response) => {
                    this.fileImportList = response.fileimport;
                    if (this.fileImportList.length > 0) {
                        this.fileImpParam = this.fileImportList[0];
                        this.querySecfileimportdetial(this.fileImpParam.fileimportid);
                    }
                })
                .catch((e) => {});
        },
        initsecfileimportcol() {
            let response = this.$tool.request({
                url: "LIS/LIS2/" + this.prono,
                method: "post",
                data: {
                    action: "initsecfileimportcol",
                    // prono: "LIS251",
                },
            });
            return response;
        },
        importReqSample() {
            if (!this.form.file) return false;
            // if (this.reqitemsList.length == 0) return false;
            var reqitemlist = [];
            if (this.allowaddreqitemflag == "1" && this.$refs.reqitemTable.getTableData().fullData.length > 0) {
                this.$refs.reqitemTable.getTableData().fullData.forEach((element) => {
                    if (element.req_itemcode) reqitemlist.push(element);
                });
            }
            let formData = new FormData();
            formData.append("action", "LIS251");
            formData.append("file", this.form.file);
            formData.append("fileimportid", this.fileImpParam.fileimportid);
            formData.append("reqitemsList", JSON.stringify(reqitemlist));
            formData.append("userno", this.$tool.getSession(this.$tool.loginuserno));
            formData.append("hospcode", this.$tool.getSession(this.$tool.selectedhis));
            var URL = this.$tool.baseUrl.replace("api", "z") + "fileupload";
            axios
                .post(URL, formData, { "Content-Type": "multipart/form-data" })
                .then((res) => {
                    let response = res.data;
                    if (response.flag == "1") {
                        this.$notify.success({
                            title: "成功",
                            message: `文件上传成功`,
                        });
                        let barcodestr = "";
                        if(!!response.keyid){
                            let barcodeList = eval("(" + response.keyid + ")");
                            if (barcodeList.length > 0 && response.autoprintbarcodes > 0) {
                                // 打印
                                barcodeList.forEach((element) => {
                                    for (var i = 0; i < response.autoprintbarcodes; i++) {
                                        barcodestr += element + ",";
                                    }
                                });
                                barcodestr = barcodestr.substring(0, barcodestr.length - 1);
                                const loading = this.$loading({
                                    lock: true,
                                    text: "打印中",
                                    spinner: "el-icon-loading",
                                    background: "rgba(0, 0, 0, 0.7)",
                                });
                                this.$pub.printbarcode(this, barcodestr, "1", this.barcodestyle, this.cxbarcodeprinter, "1");
                                loading.close();
                            }
                        }
                    } else {
                        this.$notify.error({
                            title: "错误",
                            message: response.msg,
                        });
                    }
                })
                .catch((e) => {
                    this.$alert(e, "出错");
                });
        },
        closeReqImport() {
            this.$emit("change");
        },
        changeFileimport(e) {
            if (e) {
                this.querySecfileimportdetial(e);
            } else {
                this.fileImpParam = {};
            }
        },
        querySecfileimportdetial(fileimportid) {
            this.$tool
                .request({
                    url: "LIS/LIS2/" + this.prono,
                    method: "post",
                    data: {
                        action: "querysecfileimportdetial",
                        fileimportid: fileimportid,
                    },
                })
                .then((response) => {
                    if (response.fileimport.length > 0) {
                        this.fileImpParam = response.fileimport[0];
                        if (response.allowaddreqitem.length > 0) {
                            this.allowaddreqitemflag = response.allowaddreqitem[0].optionvalue;
                        }
                        this.$forceUpdate();
                    }
                })
                .catch((e) => {});
        },
        importExcel() {
            this.$refs["upload"].$refs["upload-inner"].handleClick();
        },
        // 文件状态改变时的钩子
        fileChange(file, fileList) {
            console.log(file, fileList);
            this.filename = file.name;
            this.form.file = file.raw;
        },
        // 上传文件之前的钩子, 参数为上传的文件,若返回 false 或者返回 Promise 且被 reject，则停止上传
        beforeUploadFile(file) {
            let extension = file.name.substring(file.name.lastIndexOf(".") + 1);
            let size = file.size / 1024 / 1024;
            if (extension !== "xls" && extension !== "xlsx") {
                this.$notify.warning({
                    title: "警告",
                    message: `只能上传xls,xlsx(*xls,*xlsx)文件，且不超过10M`,
                });
            }
            if (size > 10) {
                this.$notify.warning({
                    title: "警告",
                    message: `文件大小不得超过10M`,
                });
            }
        },
        fileChangeDet(file, fileList) {
            // console.log(file, fileList)
            let extension = file.name.substring(file.name.lastIndexOf(".") + 1);
            if (extension == "xls" || extension == "xlsx") {
                this.fileImpParam.filetype = "XLS";
                this.$forceUpdate();
            }
            this.file2Xce(file).then((tab) => {
                // console.log(tab)
                if (tab && tab.length > 0) {
                    let xlscList = [];
                    if (tab.length > 0) {
                        let sheetTitle = tab[0].sheet[0]; // 此处只取第一个 sheet 的第一行数据，标题
                        for (let i in sheetTitle) {
                            xlscList.push({
                                seq: i,
                                name: sheetTitle[i],
                            });
                        }
                    }
                    // console.log(xlscList);
                    this.dealTabledata(xlscList);
                }
            });
        },
        file2Xce(file) {
            return new Promise(function (resolve, reject) {
                const reader = new FileReader();
                reader.onload = function (e) {
                    let data = e.target.result;
                    let wb = XLSX.read(data, {
                        type: "binary",
                    });
                    let result = [];
                    wb.SheetNames.forEach((sheetName) => {
                        // 多张表
                        result.push({
                            sheetName: sheetName,
                            sheet: XLSX.utils.sheet_to_json(wb.Sheets[sheetName], { header: 1, raw: false }),
                        });
                    });
                    resolve(result);
                };
                reader.readAsBinaryString(file.raw);
                // reader.readAsBinaryString(file) // 传统input方法
            });
        },
        dealTabledata(xlscList) {
            let num = [];
            let name = [];
            xlscList.forEach((element) => {
                element.seq = Number(element.seq) + 1;
                num.push(element.seq);
                name.push(element.name);
            });
            this.detTableData = xlscList;
            this.fdetTableseq = JSON.parse(JSON.stringify(num));
            this.detTableseq = num;
            this.fdetTableTitle = JSON.parse(JSON.stringify(name));
            this.detTableTitle = name;
        },
        autoCompare() {
            this.detList.forEach((element) => {
                let row = this.detTableData.filter((item) => item.name == element.datacol || item.name == element.datacolcn);
                if (row.length > 0) {
                    // row.forEach(data => {
                    //     element.filecolseq = data.seq;
                    //     element.filecolname = data.name;
                    // });
                    element.filecolseq = row[0].seq;
                    element.filecolname = row[0].name;
                }
            });
            this.fdetList = JSON.parse(JSON.stringify(this.detList));
            this.$refs.detTable.reloadData(this.detList);
        },
        saveSecFileImport() {
            if (!this.fileImpParam.fileimportname) {
                this.$pub.message(this, "模板名称不能为空！", "warning");
                return false;
            }
            let secfileimportdetList = [];
            let masterkeyflag = 0;
            this.fdetList.forEach((element) => {
                if (element.masterkey == "1") masterkeyflag++;
                if (element.filecolseq || element.filecolname || element.defaultval || element.masterkey == "1") {
                    secfileimportdetList.push(element);
                }
            });
            if (masterkeyflag != 1) {
                this.$confirm("唯一标识符只能在主表选择一个字段，且必须选择一个！", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(() => {})
                    .catch(() => {});
                return false;
            }
            this.$tool
                .request({
                    url: "LIS/LIS2/" + this.prono,
                    method: "post",
                    data: {
                        action: "savesecfileimport",
                        utype: this.fileImpParam.fileimportid ? "M" : "A",
                        secfileimport: this.fileImpParam,
                        // secfileimportdet: this.fdetList,
                        secfileimportdet: secfileimportdetList,
                        secfileimportoption: this.optionList,
                    },
                })
                .then((response) => {
                    if (response.flag == "1") {
                        this.$pub.message(this, "保存成功", "success");
                    } else {
                        this.$pub.message(this, response.errmsg, "warning");
                    }
                })
                .catch((e) => {});
        },
        closeModelSet(type) {
            if (type == 1) {
                this.modelSetDialog = false;
            }
            this.detTableData = [];
            this.fdetTableseq = [];
            this.detTableseq = [];
            this.fdetTableTitle = [];
            this.detTableTitle = [];
            this.setFileName = "importDet";
        },
        handleTabClick(tab, event) {
            this.setFileName = tab.name;
            if (tab.name == "importDet") {
                this.$refs.detTable.loadData(this.detList);
            }
        },
        showCompareData(type) {
            if (type == 1) {
                let data = this.fdetList.filter((item) => item.filecolseq || item.filecolname);
                this.detList = data;
                this.showCompareflag = false;
            } else {
                this.detList = JSON.parse(JSON.stringify(this.fdetList));
                this.showCompareflag = true;
            }
        },
        fillterDetTableseq(e) {
            let list = [];
            if (e) {
                list = this.fdetTableseq.filter((item) => item.indexOf(e) > 0);
            } else {
                list = XEUtils.clone(this.fdetTableseq, true);
            }
            console.log(list);
            this.detTableseq = list;
        },
        clearFilter() {
            this.detTableseq = JSON.parse(JSON.stringify(this.fdetTableseq));
            this.detTableTitle = JSON.parse(JSON.stringify(this.fdetTableTitle));
        },
        filecolseqChange(e, row) {
            console.log(e, row);
            row.filecolseq = e;
            this.updateFdetList(e, row, "filecolseq");
        },
        fillterDetTableTitle(e) {
            let list = [];
            if (e) {
                list = this.fdetTableseq.filter((item) => item.indexOf(e) > 0);
            } else {
                list = XEUtils.clone(this.fdetTableTitle, true);
            }
            console.log(list);
            // cb(list);
            this.detTableTitle = list;
        },
        filecolnameChange(e, row) {
            console.log(e, row);
            row.filecolname = e;
            this.updateFdetList(e, row, "filecolname");
        },
        updateFdetList(value, row, colname) {
            this.fdetList.forEach((element) => {
                if (element.datacol == row.datacol) {
                    element[colname] = value;
                }
            });
        },
        addReqitem() {
            let arr = {};
            this.$refs.reqitemTable.insert(arr).then((rows) => {
                this.$refs.reqitemTable.setCurrentRow(rows.row);
                this.$refs.reqitemTable.setActiveCell(rows.row, "req_itemcode");
                var havedata = this.$refs.reqitemTable.getTableData().fullData;
                this.reqitemsList = havedata;
            });
        },
        deleteReqitem() {
            let that = this;
            let delrow = that.$refs.reqitemTable.getCurrentRow();
            if (!delrow) return false;
            that.$confirm("你确定删除当前申请项目?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                that.$pub.message(that, "删除成功！", "success");
                // 删除不刷新表格
                var rowindex = this.$refs.reqitemTable.getTableData().fullData.indexOf(delrow);
                this.reqitemsList.splice(rowindex, 1);
                this.$refs.reqitemTable.loadData(this.reqitemsList).then(() => {
                    var havedata = this.$refs.reqitemTable.getTableData().fullData;
                    if (havedata.length > 0) {
                        if (rowindex >= havedata.length) rowindex = havedata.length - 1;
                        let selectrow = havedata[rowindex];
                        this.$refs.reqitemTable.setCurrentRow(selectrow);
                    } else {
                        this.$refs.reqitemTable.clearCurrentRow();
                    }
                });
            });
        },
        addReqpackage() {
            this.reqsheetDialog = true;
        },
        activeMethod({ row, column }) {
            if (column.property == "req_itemcode") {
                return true;
            }
        },
        changeModel() {
            var that = this;
            that.modelSetDialog = true;
            that.initsecfileimportcol().then((res) => {
                that.$tool
                    .request({
                        url: "LIS/LIS2/" + that.prono,
                        method: "post",
                        data: {
                            action: "queryfiledetail",
                            fileimportid: that.fileImpParam.fileimportid,
                        },
                    })
                    .then((response) => {
                        if (response.flag == "1") {
                            if (response.fileimport.length > 0) {
                                that.fileImpParam = response.fileimport[0];
                            }
                            // 处理 Sec_FileImportDet 表对应列字段
                            if (response.fileimportdet.length > 0) {
                                let list = res.filedetlist;
                                list.forEach((element) => {
                                    element.fileimportid = that.fileImpParam.fileimportid;
                                    let row = response.fileimportdet.find((item) => item.datacol == element.datacol);
                                    if (row) {
                                        element.filecolseq = row.filecolseq;
                                        element.filecolname = row.filecolname;
                                        element.defaultval = row.defaultval;
                                        element.masterkey = row.masterkey;
                                    }
                                });
                                that.fdetList = JSON.parse(JSON.stringify(list));
                                that.detList = list;
                            } else {
                                that.fdetList = JSON.parse(JSON.stringify(res.filedetlist));
                                that.detList = res.filedetlist;
                            }
                            that.optionList = response.fileimportoption;
                        } else {
                            that.$pub.message(that, response.errmsg, "warning");
                        }
                    })
                    .catch((e) => {});
            });
        },
        addModel() {
            this.initsecfileimportcol().then((response) => {
                this.fdetList = JSON.parse(JSON.stringify(response.filedetlist));
                this.detList = response.filedetlist;
            });
            this.optionList = reqitem.importOptionList;
            this.modelSetDialog = true;
            this.fileImpParam = {};
            this.fileImpParam.importdest = "REQ";
        },
        importExcelDet() {
            this.$refs["uploadDet"].$refs["upload-inner"].handleClick();
        },
        chooseReqsheet(type, data) {
            if (type == 1) {
                // 有数据修改回传
                console.log(data);
                this.reqitemsList = this.reqitemsList.concat(data);
            }
            this.reqsheetDialog = false;
        },
        reqitemFocus(e, row) {
            if (e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 39 || e.keyCode == 40) return false;
            if (e.target.value) {
                this.$emit("reqitemDialogShow", e, row);
            } else {
                this.$emit("reqitemDialogShow");
                this.slectReqItem();
            }
        },
        reqitemBlur(e, row) {
            this.$emit("reqitemDialogShow", e, row, true); // 第三个参数为 true，失去焦点前判断有没有对应的 code 的数据
        },
        slectReqItem(e) {
            let row = this.$refs.reqitemTable.getCurrentRow();
            if (e) {
                row.req_itemcode = e.req_itemcode;
                row.req_itemname = e.req_itemname;
                row.specimen_code = e.specimen_code;
                row.specimen_name = e.specimen_name;
            } else {
                row.req_itemcode = "";
                row.req_itemname = "";
                row.specimen_code = "";
                row.specimen_name = "";
            }
        },
    },
};
</script>

<style scoped>
.import-content {
    color: #000000;
    font-size: 12px;
}
.import-btn {
    text-align: right;
    /* margin: 0 0 10px 0; */
}
.import-info {
    margin: 0 0 5px 0;
}
.import-param {
    display: flex;
    margin: 5px 0;
}
.search-title {
    width: 75px;
    margin: 0 5px 0 0;
    text-align: right;
    line-height: 26px;
}
.input-upload {
    width: calc(100% - 153px);
    margin-right: 5px;
}
.upload-button {
    height: 24px;
    margin: 0 10px;
    display: none;
}
.upload-button div {
    height: 24px;
}
.reqitem-content {
    display: flex;
    height: 250px;
}
.reqitem-table {
    width: 490px;
    height: 220px;
}
.reqitem-table table {
    height: 100%;
}
.reqitem-btn {
    text-align: center;
    margin: auto 0 auto auto;
}
.reqitem-btn div {
    margin: 15px 0;
}
/deep/ .reqitem-btn .el-button {
    width: 90px;
}
.modelset-content {
    display: flex;
    color: #000000;
    font-size: 12px;
}
.modelset-left {
    width: 205px;
}
.model-param {
    margin: 3px 0;
}
.model-text {
    width: 70px;
    margin: 0 5px 0 0;
    text-align: right;
    display: inline-block;
}
.modelset-right {
    width: 710px;
    height: 506px;
    margin-left: 10px;
}
/deep/ .modelset-right .el-tabs {
    height: calc(100% - 35px);
}
/deep/ .modelset-right .el-tabs__header {
    margin: 0 0 5px !important;
}
/deep/ .modelset-right .el-tabs__content {
    position: relative !important;
    top: 0 !important;
    height: calc(100% - 33px);
}
.eltab-content,
.eltab-content table,
.mytable-style {
    height: 100%;
}
/deep/ .eltab-content .vxe-table .vxe-header--column {
    line-height: 19px !important;
}
.modelset-btn {
    margin: 5px 0;
    text-align: center;
}
/deep/ .el-input.is-disabled .el-input__inner {
    color: #000000 !important;
}
</style>